<?php
/**
 * Created by UrchinDESIGN.
 * User: UrchinStarz
 * Date: 2018/4/20
 * Time: 23:02
 */

class Code{
    private $img;
    private $width=100;
    private $height=30;
    private $bgColor='#ffffff';
    private $code;
    private $codeStr='23456789abcdefghijklmnopqrstuvwxyz';
    private $codeLen =4;
    private $font;
    private $fontSize=16;
    private $fontColor='';

    public function __construct()
    {

    }


    //创建验证码图片
    public function make()
    {
        if(empty($this->font)){
            $this->font =dirname(__FILE__) .'/georgia.ttf';
        }
        $this->create(); //生成验证码
        header("Content-type:image/png");
        imagepng($this->img);
        imagedestroy($this->img);
        exit;
    }
    //设置字体文件
    public function font($font){

    }
    //设置文字大小
    public function fontSize($fontSize)
    {
        $this->fontSize=$fontSize;
        return $this;
    }

    public function fontColor($fontColor){
        $this->fontColor=$fontColor;
        return $this;
    }

    public function num($num){
        $this->codeLen=$num;
        return $this;
    }

    public function width($width){
        $this->width=$width;
        return $this;
    }
    public function height($height){
        $this->height=$height;
        return $this;
    }

    public function background($color){
        $this->bgColor=$color;
        return $this;
    }

    public function get()
    {
        return $_SESSION['code'];
    }

    public function create()
    {
        $code='';
        for($i=0;$i<$this->codeLen;$i++){
            $code.=$this->codeStr[mt_rand(0,strlen($this->codeStr)-1)];
        }
        $this->code =strtoupper($code);
        $_SESSION['code']=$this->code;

//        $charset_len = strlen($this->codeStr)-1;
//        for ($i=0; $i<$this->codeLen; $i++) {
//            $code .= $this->codeStr[rand(1, $charset_len)];
//        }
//        $this->code = $code;

    }



}